package common.annotation.demo2;

import java.lang.reflect.Field;
import java.util.Arrays;

public class Person {

    @Range(min = 1,max = 20)
    public String name;

    @Range(max = 10)
    public String city;

    public Person() {
    }

    public Person(String name, String city) {
        this.name = name;
        this.city = city;
    }

    public static void check(Person person) {

        Field[] fields = person.getClass().getFields();

        Arrays.stream(fields).forEach(item ->{

            if ("name".equals(item.getName())) {

                Range range = item.getAnnotation(Range.class);
                if (null == range
                ) {
                    return;
                }

                try {
                    String name = (String)item.get(person);
                    if (range.min() > name.length() || name.length() > range.max()) {
                        throw new RuntimeException("字符超长");
                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

                System.out.println(item.getName()+"验证通过");

            }
        });


    }

    public static void main(String[] args) {

        System.out.println("test");

        Person  person= new Person("zhangsan","kdjfdkfjdkfjdkjfdkjfkdjfdkjfdkjfdkjfdkjf");

        check(person);
    }
}
